﻿package net.wangit.adm.group;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.jdbc.DB;
import net.wangit.support.action.TransactionAction;
import net.wangit.user.admin.AdminCache;
import net.wangit.user.group.UserGroupCache;
import net.wangit.util.Checker;

public class DeleteGroupAction extends TransactionAction {

	private static final String CONFIRM = "select * from AGROUP where parentid=?";
	private static final String GROUP_DELETE = "delete from AGROUP where groupid=?";
	private static final String ADMINISTRATOR_DELETE = "delete from AADMINGROUP where groupid=?";
	private static final String ROLE_DELETE = "delete from AGROUPROLE where groupid=?";
	private static final String USER_DELETE = "delete from AUSERGROUP where groupid=?";
	private static final String LEVEL_DELETE = "delete from AGROUPLEVEL where upperid=? or lowerid=? ";

	public DeleteGroupAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		String groupId = ac.getRequestParameter("groupid");
		net.wangit.MapList list = db.query(
				"select * from AGROUP where parentid=?", groupId, 2);
		if (!Checker.isEmpty(list)) {
			ac.getActionResult().setSuccessful(false);
			ac.getActionResult().addError(
					LabelUtil.get(ac, "adm", "group.has_subnodes", groupId));
		} else {
			db.execute("delete from AGROUP where groupid=?", groupId, 2);
			db.execute("delete from AADMINGROUP where groupid=?", groupId, 2);
			db.execute("delete from AGROUPROLE where groupid=?", groupId, 2);
			db.execute("delete from AUSERGROUP where groupid=?", groupId, 2);
			db.execute("delete from AGROUPLEVEL where upperid=? or lowerid=? ",
					new String[] { groupId, groupId }, new int[] { 2, 2 });
			UserGroupCache.clear(groupId);
			AdminCache.clearAll();
		}
	}
}
